package cn.edu.neusoft.yuejian.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import cn.edu.neusoft.yuejian.R;
import cn.edu.neusoft.yuejian.frgment.JlbFragment;
import cn.edu.neusoft.yuejian.frgment.PxFragment;
import cn.edu.neusoft.yuejian.frgment.StFragment;
import cn.edu.neusoft.yuejian.frgment.SyFragment;
import cn.edu.neusoft.yuejian.frgment.WdFragment;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private LinearLayout mpx;
    private LinearLayout mjlb;
    private LinearLayout msy;
    private LinearLayout mst;
    private LinearLayout mwd;

    private ImageButton mImgpx;
    private ImageButton mImgjlb;
    private ImageButton mImgsy;
    private ImageButton mImgst;
    private ImageButton mImgwd;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(2);
    }
    private void initEvent()
    {
        mpx.setOnClickListener(this);
        mjlb.setOnClickListener(this);
        msy.setOnClickListener(this);
        mst.setOnClickListener(this);
        mwd.setOnClickListener(this);

    }

    private void initView()
    {
        mpx= (LinearLayout) findViewById(R.id.id_tab_px);
        mjlb = (LinearLayout) findViewById(R.id.id_tab_jlb);
        msy = (LinearLayout) findViewById(R.id.id_tab_sy);
        mst = (LinearLayout) findViewById(R.id.id_tab_st);
        mwd = (LinearLayout) findViewById(R.id.id_tab_wd);

        mImgpx = (ImageButton) findViewById(R.id.id_tab_px_img);
        mImgjlb = (ImageButton) findViewById(R.id.id_tab_jlb_img);
        mImgsy  = (ImageButton) findViewById(R.id.id_tab_sy_img);
        mImgst = (ImageButton) findViewById(R.id.id_tab_st_img);
        mImgwd = (ImageButton) findViewById(R.id.id_tab_wd_img);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new PxFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mImgpx.setImageResource(R.drawable.ic_px_liang);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new JlbFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);

                }
                mImgjlb.setImageResource(R.drawable.ic_jlb_liang);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new SyFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mImgsy.setImageResource(R.drawable.ic_sy_liang);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new StFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mImgst.setImageResource(R.drawable.ic_st_liang);
                break;
            case 4:
                if (mTab05 == null)
                {
                    mTab05 = new WdFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else
                {
                    transaction.show(mTab05);
                }
                mImgwd.setImageResource(R.drawable.ic_sz_liang);
                break;
            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_px:
                setSelect(0);
                break;
            case R.id.id_tab_jlb:
                setSelect(1);
                break;
            case R.id.id_tab_sy:
                setSelect(2);
                break;
            case R.id.id_tab_st:
                setSelect(3);
                break;
            case R.id.id_tab_wd:
                setSelect(4);
                break;
            default:
                break;
        }
    }
    private void resetImgs()
    {
        mImgpx.setImageResource(R.drawable.ic_px_an);
        mImgjlb.setImageResource(R.drawable.ic_jlb_an);
        mImgsy.setImageResource(R.drawable.ic_sy_an);
        mImgst.setImageResource(R.drawable.ic_st_an);
        mImgwd.setImageResource(R.drawable.ic_sz_an);
    }
}
